package lion.framework.core.web;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

import org.apache.commons.lang.StringUtils;

import lion.framework.core.web.exception.WebException;
import lion.framework.core.web.path.PathNodeExecutor;

public class WrappedErrorResponse extends HttpServletResponseWrapper {

	private HttpServletRequest request;

	public WrappedErrorResponse(HttpServletResponse response) {

		super(response);
	}

	public WrappedErrorResponse(HttpServletRequest request, HttpServletResponse response) {

		this(response);
		this.request = request;
	}

	@Override
	public void sendError(int sc, String msg) throws IOException {

		if (StringUtils.isBlank(msg)) {
			if (sc == 404) {
				msg = "File Not Found " + this.request.getRequestURI();
			} else if (sc == 500) {
				msg = "Service Not Available ";
			}
		}

		try {
			PathNodeExecutor.onException(request, this, new WebException(sc, msg));
		} catch (Exception e) {
		}
	}

	@Override
	public void sendError(int sc) throws IOException {

		this.sendError(sc, null);
	}

}
